<?php
/**
	会员观看广告视频类
	
	@author xiaoming <maixiaoming@9qi.cn>
*/
class ViewAction extends BaseAction 
{
	
	/**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	public function index()
	{
		$this->_checkLogin();  //检查登录
		$Video = M('video');
		$id = intval($_GET['id']);
		if (!empty($id)){
			$result = $Video->where("video_id=$id")->find();
			if($result){
			
			/*=========================================================
			目前是打开广告可看到增加积分，运行后需把此取消
			*/
				//给该会员加上相应的广告积分
				$this->_addPoints($this->userId, $id, $result['point']);
				//记录日志
				$this->_logUserView($this->userId, $id, $result['point']);
			/*=========================================================*/
			
				$this->assign('u_id',$this->userId);
				$this->assign('video',$result);
				$this->display();
			} else {
				$this->redirect('index/index', '', 3, 'Video Not Found!');
			}
		} else {
			$this->redirect('index/index');
		}
	}
	
	/**
		会员观看视频广告后处理程序
	*/
	public function action()
	{
		$this->_checkLogin();  //检查登录
		$video_id = isset($_POST['video_id']) ? intval($_POST['video_id']) : 0;
		$Video = M('video');
		
		if (!empty($video_id )){
			if ( $this->_checkViewed($this->userId, $video_id) === false ) {  //今天还没观看此视频，可继续操作
				$result = $Video->where("video_id=$video_id")->find();
				if($result){
					//给该会员加上相应的广告积分
					$this->_addPoints($this->userId, $video_id, $result['point']);
					//记录日志
					$this->_logUserView($this->userId, $video_id, $result['point']);
					
					$this->redirect('index');
					
				} else {
					//无该视频处理
				}
			} else {
				//已经看过此视频处理
				echo "<script>alert('今天已经看过此视频');</script>";
				$this->redirect('index');
			}

		} else{
			$this->redirect('index');
		}
	}
	
	/**
		给会员加上相应的广告积分
		广告视频观看次数+1
		
		@access private
		@param  int $u_id  会员ID
				int $video_id 广告视频ID
				int $point 广告积分
		@return void
	*/
	private function _addPoints($u_id, $video_id, $point)
	{
		$User = M('User');
		$Video = M('Video');

		$User->setInc('u_points', "u_id=$u_id", $point); //会员加积分
		$Video->setInc('view_counts',"video_id=$video_id", 1); //广告观看次数+1

		
	}
	
	/**
		记录会员观看视频广告日志
		
		@access private
		@param  int $u_id  会员ID
				int $video_id 广告视频ID
				int $point 广告积分
		@return void
	*/
	private function _logUserView($u_id, $video_id, $point) 
	{
		Load('extend');
		$Log = M('log');
		$data['u_id'] = $u_id;
		$data['video_id'] = $video_id;
		$data['view_time'] = time();
		$data['point'] = $point;
		$data['ip'] = get_client_ip();
		$Log->add($data);
	}
	

		
		
}
?>